package com.manager.core.shiro.core.service;

import java.util.*;

import com.manager.core.shiro.core.entity.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.manager.commons.mybatis.dao.GenericDao;
import com.manager.commons.mybatis.service.GenericService;
import com.manager.core.shiro.core.dao.ShiroDao;

/**
 * ShiroService
 *
 * @author H
 */
@Service
public class ShiroService extends GenericService<ShiroPermission> {

    @Autowired
    protected ShiroDao jdbcDao;

    @Override
    public GenericDao<ShiroPermission> getDao() {
        return jdbcDao;
    }

    @Override
    public ShiroPermission getModel() {
        return new ShiroPermission();
    }

    /**
     * 查询用户角色
     *
     * @param account
     * @return
     */
    public ShiroUserRole queryUserRoles(String account) {
        Map<String, Object> map = new HashMap<>();
        map.put("account", account);
        ShiroUserRole list = jdbcDao.queryUserRoles(map);
        return list;
    }

    public UserView queryUserView(String account) {
        return jdbcDao.queryUserView(account);
    }


    /**
     * 查询超级权限下用户的所有菜单
     *
     * @return
     */
    public List<ShiroRolePermission> queryPermissionsAllThree() {
        List<ShiroRolePermission> list = jdbcDao.queryPermissionAllThree(null);
        queryPermissionsAllThree(list);
        return list;
    }

    private void queryPermissionsAllThree(List<ShiroRolePermission> list) {
        for (ShiroRolePermission permission : list) {
            if (permission.getPpSub() == 1) {//有子菜单的情况下才继续查询下一集菜单
                List<ShiroRolePermission> childList = jdbcDao.queryPermissionAllThree(permission.getPerCode());
                permission.setItems(childList);
                queryPermissionsAllThree(childList);
            }
        }
    }

    /**
     * 查询权限
     *
     * @param map
     * @return
     */
    public List<ShiroRolePermission> queryPermissionThree(Map<String, Object> map) {
        List<ShiroRolePermission> listPermission = jdbcDao.queryPermissionThree(map);
        queryPermissionThree(listPermission, map);
        return listPermission;
    }

    private void queryPermissionThree(List<ShiroRolePermission> list, Map<String, Object> map) {
        for (ShiroRolePermission permission : list) {
            if (permission.getPpSub() == 1) {//有子菜单的情况下才继续查询下一集菜单
                map.put("parentCode", permission.getPerCode());
                List<ShiroRolePermission> childList = jdbcDao.queryPermissionThree(map);
                permission.setItems(childList);
                queryPermissionThree(childList, map);
            }
        }
    }

    public List<UserView> queryUserAll() {
        return jdbcDao.queryUserAll();
    }

    public List<UserView> queryRoleAll() {
        return jdbcDao.queryRoleAll();
    }

    /**
     * 查询角色权限
     *
     * @param roleCode
     * @return
     */
    public Set<String> queryRolePermission(String roleCode) {
        List<ShiroRolePermission> list = jdbcDao.queryRolePermission(new HashMap<String, Object>() {
            {
                put("roleCode", roleCode);
            }
        });
        Set<String> strings = new HashSet<String>();
        for (ShiroRolePermission s : list) {
            strings.add(s.getPerAuth());
        }
        return strings;
    }

    /**
     * 查询角色权限
     *
     * @return
     */
    public Set<String> queryPermissionAll() {
        List<ShiroRolePermission> list = jdbcDao.queryPermissionAll();
        Set<String> strings = new HashSet<String>();
        for (ShiroRolePermission s : list) {
            strings.add(s.getPerAuth());
        }
        return strings;
    }

    public LoginUser queryAccountInfo(String account) {
        LoginUser loginUser = jdbcDao.login(new HashMap<String, Object>() {
            {
                put("account", account);
            }
        });
        return loginUser;
    }

    /**
     * 登录
     *
     * @param account
     * @param pwd
     * @param ip
     * @return
     */
    public LoginUser login(final String account, String pwd, final String ip) {
        if (StringUtils.isBlank(account)) {
            return null;
        }
        if (StringUtils.isBlank(pwd)) {
            return null;
        }
        try {
            LoginUser loginUser = jdbcDao.login(new HashMap<String, Object>() {
                private static final long serialVersionUID = 623012754587947505L;

                {
                    put("account", account);
                }
            });
            if (null != loginUser) {
                if (pwd.equals(loginUser.getPassword())) {
                    return new LoginUser(loginUser.getAccount(), loginUser.getPassword());
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public Set<String> getPermissions(String account) {
        Set<String> strings = new HashSet<String>();
        List<ShiroRolePermission> list = jdbcDao.queryAccountPermission(account);
        for (ShiroRolePermission s : list) {
            strings.add(s.getPerCode());
        }
        return strings;
    }

    public Set<String> getUserRoles(String account) {
        Set<String> strings = new HashSet<>();
        ShiroUserRole list = queryUserRoles(account);
        strings.add(list.getRoleCode());
        return strings;
    }

    /**
     * 获取当前用户授权的动物类别
     *
     * @param usId
     * @return
     */
    public List<String> queryUserCategoryStr(String usId) {
        return jdbcDao.queryUserCategoryStr(usId);
    }

    /**
     * 获取当前用户授权的动物馆舍
     *
     * @param usId
     * @return
     */
    public List<String> queryUserLibraryStr(String usId) {
        return jdbcDao.queryUserLibraryStr(usId);
    }
}